package com.app.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.webkit.WebView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.app.R
import android.view.Menu
import com.app.NewsApplication.Companion.context
import com.app.NewsApplication.Companion.if_login
import com.app.NewsApplication.Companion.loginUser
import com.app.util.DBOpenHelper
import com.app.util.showToast
import kotlin.concurrent.thread
import kotlin.properties.Delegates


class DetailActivity : AppCompatActivity() {
//    lateinit var intent1: Intent
    lateinit var dbOpenHelper: DBOpenHelper
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail)
        dbOpenHelper = DBOpenHelper(this)
//        intent1 = this.intent
        var newsId = intent.getLongExtra("news_id", -1);
        if(!dbOpenHelper.isHised(loginUser,newsId)){
            dbOpenHelper.addHistory(loginUser,newsId)
        }

        // 设置标题栏
        val toolbar = findViewById<Toolbar>(R.id.detail_tool_bar)
        setSupportActionBar(toolbar)
        toolbar.setOnMenuItemClickListener {
            when (it.itemId) {
                R.id.detail_collect -> {
                    if(if_login == 0){
                        var intent_login = Intent(context,LoginActivity::class.java)
                        startActivity(intent_login)
                    }
                    else if(it.title.equals("收藏")) {
                        dbOpenHelper.addCollect(loginUser, newsId)
                        "收藏成功！".showToast()
                        it.title = "取消收藏"
                    } else{
                        dbOpenHelper.deleteCollect(loginUser, newsId)
                        "取消收藏成功".showToast()
                        it.title = "收藏"
                    }
                }
            }
            false
        }

        // 去掉默认的标题
        title = ""
        // 设置标题的居中文字
        val realTitle = findViewById<TextView>(R.id.detail_real_title)
        realTitle.text = "DiliDili"
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        // 打开新闻网页
        val webView = findViewById<WebView>(R.id.news_web_view)
        val url = intent.getStringExtra("url=")
        if (url != null) {
            webView.loadUrl(url)
        }
    }

    // 给Activity加载标题栏的菜单项
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        //写一个menu的资源文件.然后创建就行了.
        menuInflater.inflate(R.menu.detail_tool_bar_menu, menu)
        var newsId = intent.getLongExtra("news_id", -1);
        if(dbOpenHelper.isCollected(loginUser, newsId)) {
            menu?.findItem(R.id.detail_collect)?.setTitle("取消收藏")
        } else {
            menu?.findItem(R.id.detail_collect)?.setTitle("收藏")
        }
        return super.onCreateOptionsMenu(menu)
    }

    //给系统自带的home按钮(小箭头)添加点击事件：销毁本页面返回上一级
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }


}